//
//  MainView.m
//  RtWeibo
//
// 

#import "MainView.h"
#include "RtBase.h"
#include "RtWindowGc.h"
#include "RtBitmapGc.h"
#include "RtFont.h"
#include "RtPathUtil.h"
#include "RtImage.h"
#include "RtWeiboApp.h"
#include "RtWeiboAppObserver.h"

bool g_bFirstDraw=true;

@implementation MainView

- (id)initWithCoder:(NSCoder *)coder {
	
	self = [super initWithCoder:coder];
	if (self) {
        m_pRtWeiboAppObserver=new CRtWeiboAppObserver();
        m_pRtWeiboAppObserver->m_pMainView=self;
        
		CGRect rectApp=[UIScreen mainScreen].applicationFrame;        
        CRtRect rectClient(0,0, CGRectGetWidth(rectApp), CGRectGetHeight(rectApp));
        
        m_pRtWeiboApp=CreateUiManager();
        
        CRtWindow* pWnd=new CRtWindow(self);
        CRtControl* pParent=new CRtControl(pWnd);
        pParent->SetRect(rectClient);
        
        m_pRtWeiboApp->SetPlatWindow(pParent);
        m_pRtWeiboApp->SetOsType(ERtOs_iOS);
        m_pRtWeiboApp->SetLayoutType(ERtLayout_PAD);
        m_pRtWeiboApp->SetObserver(m_pRtWeiboAppObserver);
        m_pRtWeiboApp->SetScreenRect(rectClient);
        m_pRtWeiboApp->Start();
        
        m_pNSTimer=nil;
	}
	return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        m_pNSTimer=nil;
    }
    
    return self;
}

- (void)dealloc
{
    [super dealloc];
    
    if(m_pRtWeiboApp)
    {
        m_pRtWeiboApp->Stop();
        delete m_pRtWeiboApp;
        m_pRtWeiboApp=NULL;
    }
    if(m_pRtWeiboAppObserver)
    {
        delete m_pRtWeiboAppObserver;
        m_pRtWeiboAppObserver=NULL;
    }
    if(m_pNSTimer)
    {
        [m_pNSTimer release];
        m_pNSTimer=nil;
    }
}

// Only override drawRect: if you perform custom drawing.  
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGRect rectApp=[UIScreen mainScreen].applicationFrame;        
    CRtRect rectClient(0,0, CGRectGetWidth(rectApp), CGRectGetHeight(rectApp));
    
    CRtWindowGc wndGc;
    CGAffineTransform myTr = CGAffineTransformMake(1, 0, 0, -1, 0, rectClient.Height());
    CGContextConcatCTM(UIGraphicsGetCurrentContext(), myTr);
    m_pRtWeiboApp->UiDraw(wndGc, rectClient);
    }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	// We only support single touches, so anyObject retrieves just that touch from touches
	UITouch *touch = [touches anyObject];
	CGPoint pt=[touch locationInView:self];
//    NSLog(@"touchesBegan(x=%f,y=%f)",pt.x,pt.y);
    
    TRtPointerEvent ptEvent;
	ptEvent.uiType=TRtPointerEvent::EButton1Down;
	ptEvent.iPosition.m_nX=pt.x;
	ptEvent.iPosition.m_nY=pt.y; 
	
	if(m_pRtWeiboApp)
		return m_pRtWeiboApp->HandlePointerEventL(ptEvent);
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
	CGPoint pt=[touch locationInView:self];
//    NSLog(@"touchesMoved(x=%f,y=%f)",pt.x,pt.y);
    
    TRtPointerEvent ptEvent; 
	ptEvent.uiType=TRtPointerEvent::EMove;
	ptEvent.iPosition.m_nX=pt.x;
	ptEvent.iPosition.m_nY=pt.y;
	
	if(m_pRtWeiboApp)
		return m_pRtWeiboApp->HandlePointerEventL(ptEvent);

}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [touches anyObject];
	CGPoint pt=[touch locationInView:self];
//    NSLog(@"touchesEnded(x=%f,y=%f)",pt.x,pt.y);
    
    TRtPointerEvent ptEvent;
	ptEvent.uiType=TRtPointerEvent::EButton1Up;
	ptEvent.iPosition.m_nX=pt.x;
	ptEvent.iPosition.m_nY=pt.y;
	
	if(m_pRtWeiboApp)
		return m_pRtWeiboApp->HandlePointerEventL(ptEvent);

}


- (void)exitApplication {
    m_pNSTimer = [NSTimer scheduledTimerWithTimeInterval:0.001
                                                  target:self
                                                selector:@selector(handleMaxShowTimer:)
                                                userInfo:nil
                                                 repeats:NO];
}

- (void)handleMaxShowTimer:(NSTimer *)theTimer
{
    if(m_pRtWeiboApp)
    {
        m_pRtWeiboApp->Stop();
        delete m_pRtWeiboApp;
        m_pRtWeiboApp=NULL;
    }
    if(m_pRtWeiboAppObserver)
    {
        delete m_pRtWeiboAppObserver;
        m_pRtWeiboAppObserver=NULL;
    }
//    NSLog(@"Clean resource finished.");
    
    [UIView beginAnimations:@"exitApplication" context:nil];  
    [UIView setAnimationDuration:0.5];  
    [UIView setAnimationDelegate:self];  
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.window cache:NO];  
    [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];  
    self.window.bounds = CGRectMake(0, 0, 0, 0);  
    [UIView commitAnimations];
}

- (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {  
    if ([animationID compare:@"exitApplication"] == 0) { 
        
        exit(0); 
//        if([[UIApplication sharedApplication] respondsToSelector:@selector(terminateWithSuccess)])
//            [[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
    }
}

@end
